<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>叩丁狼权限管理系统</title>
    <#include "../common/header.ftl" >
    <script type="text/javascript" src="/jquery/plugins/jquery.form.js"></script>
    <script type="text/javascript" src="/jquery/plugins/jquery.bootstrap.min.js"></script>
    <script type="text/javascript" src="/jquery/plugins/treeview/bootstrap-treeview.min.js"></script>
    <link rel="stylesheet" href="/jquery/plugins/treeview/bootstrap-treeview.min.css">
    <script>
        $(function () {
            // 编辑按钮
            $(".btn-input").click(function () {
                //先清空表单中的数据
                $("#editForm input").val("");
                //取出按钮上绑定的json数据
                var json = $(this).data("json");
                if (json) {  // 编辑按钮
                    //回显数据
                    $("input[name=id]").val(json.id);
                    $("input[name=name]").val(json.name);
                    $("input[name='parent.id']").val(json.parentId);
                    $("input[name='parent.name']").val(json.parentName);
                }else {   // 保存按钮
                    // 获取选中的节点数据
                    var selected = $("#treeview").treeview("getSelected");
                    if(selected.length>0){
                        //回显到模态框
                        $("input[name='parent.id']").val(selected[0].id);
                        $("input[name='parent.name']").val(selected[0].text);
                    }
                }
                $("#inputModal").modal("show");
            })

            //提交表单
            $(".btn-submit").click(function () {
                //将表单的提交方式修改为ajax异步提交
                $("#editForm").ajaxSubmit(function (data) {
                    if(data.success){
                        $.messager.alert("温馨提示","操作成功!");
                        setTimeout(function () {
                            window.location.reload();
                        },500);
                    }
                })
            });

            // 设为推荐按钮
            $(".btn-state").click(function () {
                var json = $(this).data("json");
               $.get("/regions/state.do",json,function (data) {
                    if(data.success){
                        $.messager.alert("温馨提示","操作成功!");
                        setTimeout(function () {
                            window.location.reload();
                        },500);
                    }
                })
            })

            //默认查询所有的根地区
            $.get("/regions/listByParentId.do",{type:"tree"} ,function (data) {
                $("#treeview").treeview({
                    data: [{text: '全部地区', nodes: data}],
                    showTags: true, /*显示推荐标签*/
                    lazyLoad: function (node) {//懒加载查询
                        //$("#addArea").attr("data-json",node.id);
                        //查询该节点下的子节点
                        $.get("/regions/listByParentId.do", {parentId: node.id, type: "tree"}, function (data) {
                            //调用插件的方法添加节点(第一个参数是要添加的节点,第二个参数是要添加到哪里(父节点))
                            $('#treeview').treeview('addNode', [data, node]);
                        })
                    },
                    onNodeSelected: function(event, data) {//选中节点的事件
                        $.get("/regions/listByParentId.do",{parentId:data.id},function (data) {
                            //清空tbody内容
                            $("#regionTb tbody").empty();
                            $.each(data,function (index, ele) {
                                var tr = $("#template tr").clone(true);//把事件也克隆过去
                                //设置内容
                                $(tr).find("td:nth-child(1)").html(index+1);
                                $(tr).find("td:nth-child(2)").html(ele.name);
                                /*if(ele.state==1){
                                   $("tr .btn-state").html("");
                                   $("tr .btn-state").html("取消推荐");
                                }*/
                                //判断地区的状态
                                if(ele.state==1){
                                $(tr).find("a:last").html("取消推荐");
                                }
                                // 给a标签绑定json字符串
                                $(tr).find("a").attr("data-json",ele.json);
                                $("#regionTb tbody").append(tr);
                            })
                        })
                    }
                });
            });

        });
    </script>
</head>
<body>
<#--模板-->
<table id="template" style="display: none">
    <tr>
        <td></td>
        <td></td>
        <td>
            <a class="btn btn-input" >
                <span class="glyphicon glyphicon-pencil"></span>编辑
            </a>
            <a class="btn btn-state" >
               设为推荐
            </a>
        </td>
    </tr>
</table>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-3">
            <#assign currentMenu="regions"/>
            <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">地区管理</h1>
                </div>

            </div>

            <div class="row">
                <div class="col-sm-4">
                    <div id="treeview"></div>
                </div>
                <div class="col-sm-8">
                    <table class="table table-striped table-hover" id="regionTb">
                        <!--添加地区--->
                        <input type="button" id="addArea" class="btn btn-success btn-input" value="添加地区">
                        <thead>
                        <tr>
                            <th>编号</th>
                            <th>名称</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        <#--编辑模态框-->
            <div class="modal fade" id="inputModal" tabindex="-1" role="dialog">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                            </button>
                            <h4 class="modal-title">编辑</h4>
                        </div>
                        <div class="modal-body">
                            <form class="form-horizontal" action="/regions/saveOrUpdate.do" method="post" id="editForm">
                                <input type="hidden" name="id">
                                <div class="form-group">
                                    <label for="name" class="col-sm-2 control-label">地区名称：</label>
                                    <div class="col-sm-6">
                                        <input type="text" class="form-control" id="name" name="name"
                                               placeholder="请输入地区名称">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="sn" class="col-sm-2 control-label">上级地区：</label>
                                    <div class="col-sm-6">
                                        <input type="hidden" class="form-control" id="parentId" name="parent.id" ><br/>
                                        <input type="text" disabled class="form-control" id="parentName" name="parent.name"
                                               placeholder="全部地区">
                                    </div>
                                </div>
                            </form>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                            <button type="button" class="btn btn-primary btn-submit">保存</button>
                        </div>
                    </div><!-- /.modal-content -->
                </div><!-- /.modal-dialog -->
            </div><!-- /.modal -->
        <#--分页模板-->
        <#--<#include "../common/page.ftl" />-->
        </div>
    </div>
</div>

</body>
</html>